﻿using System.Collections.Generic;

namespace TechMadeira.Workflows
{
    public class OrderWorkflow
    {
        #region Singleton usage: OrderWorkflow.Instance
        private static readonly OrderWorkflow mInstance = new OrderWorkflow();

        static OrderWorkflow()
        {
        }

        public static OrderWorkflow Instance { get { return mInstance; } }

        #endregion

        private List<IOrderState> mStates = new List<IOrderState>(); 

        private OrderWorkflow()
        {
            Ordered = new OrderStateOrdered();
            mStates.Add( Ordered );

            SoldOut = new OrderStateSoldOut();
            mStates.Add( SoldOut );

            Prepared = new OrderStatePrepared();
            mStates.Add( Prepared );

            Shipped = new OrderStateShipped();
            mStates.Add( Shipped );

            Delivered = new OrderStateDelivered();
            mStates.Add( Delivered );

            Finished = new OrderStateFinished();
            mStates.Add( Finished );

            Returned = new OrderStateReturned();
            mStates.Add( Returned );

            Canceled = new OrderStateCanceled();
            mStates.Add( Canceled );
        }

        public IOrderState GetOrderByName( string aName )
        {
            foreach ( var state in mStates )
            {
                if ( state.Name == aName ) return state;
            }
            return null;
        }

        public IOrderState Ordered { get; private set; }
        public IOrderState SoldOut { get; private set; }
        public IOrderState Prepared { get; private set; }
        public IOrderState Shipped { get; private set; }
        public IOrderState Delivered { get; private set; }
        public IOrderState Returned { get; private set; }
        public IOrderState Finished { get; private set; }
        public IOrderState Canceled { get; private set; }

        public IOrderState Current { get; set; }
        
        public void Order()
        {
            if ( Current == null ) return;
            Current.Order();
        }

        public void Wait()
        {
            if ( Current == null ) return;
            Current.Wait();
        }

        public void Prepare()
        {
            if ( Current == null ) return;
            Current.Prepare();
        }

        public void Dispatch()
        {
            if ( Current == null ) return;
            Current.Dispatch();
        }

        public void Deliver()
        {
            if ( Current == null ) return;
            Current.Deliver();
        }

        public void Accept()
        {
            if ( Current == null ) return;
            Current.Accept();
        }

        public void Return()
        {
            if ( Current == null ) return;
            Current.Return();
        }

        public void Refund()
        {
            if ( Current == null ) return;
            Current.Refund();
        }

    }
}
